package com.smarth.zhizulian.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.activity.AgentWebActivity;
import com.smarth.zhizulian.activity.LoginActivity;
import com.smarth.zhizulian.activity.WebViewActivity;
import com.smarth.zhizulian.adapter.MyPagerAdapter;
import com.smarth.zhizulian.adapter.ViewPagerAdapter;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.app.EventFragment;
import com.smarth.zhizulian.bean.activity.ActivityStatus;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.popupwindow.MissionHongBaoDialog;
import com.smarth.zhizulian.utils.GsonUtils;
import com.smarth.zhizulian.utils.KumaDialogFactory;
import com.smarth.zhizulian.utils.KumaLog;
import com.smarth.zhizulian.utils.KumaToast;
import com.smarth.zhizulian.utils.SPUtils;
import com.smarth.zhizulian.utils.TimeUtils;
import com.smarth.zhizulian.utils.UserUtils;
import com.smarth.zhizulian.utils.listhelper.RefreshHelper;
import com.smarth.zhizulian.utils.listhelper.impl.adapter.PageDataAdapter;
import com.smarth.zhizulian.utils.listhelper.impl.model.HttpPageDataModel;
import com.smarth.zhizulian.view.LoadingLayout;
import com.jude.rollviewpager.RollPagerView;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.rd.PageIndicatorView;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * Created by 18865 on 2017/12/20.
 */

public class InviteFragment extends EventFragment {
    @BindView(R.id.tl_tablayout)
    SmartTabLayout tlTablayout;
    @BindView(R.id.vp_viewpager)
    ViewPager vpViewpager;
    MyPagerAdapter pagerAdapter;
    @BindView(R.id.vp_banner)
    RollPagerView vpBanner;
    @BindView(R.id.pi_indicator)
    PageIndicatorView piIndicator;
    @BindView(R.id.ll_loadinglayout)
    LoadingLayout llLoadinglayout;
    private Disposable mDisposable;
    private RefreshHelper<ActivityStatus> refreshHelper;
    private boolean mCanShowActivity = false;    //是否满足展示条件
    private TextView tv_lefttime;
    private ImageView iv_huodong;
    private boolean mIsShow = false;    //当前页面是否在展示

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setBKView(R.layout.fragment_invite);
        initTab();
//        initBanner();
        initGallary();
    }

    private void initGallary() {
        List<View> views = new ArrayList<>();

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_invite_gallary, null);
        view.setOnClickListener(new OnClickBanner());

        View view2 = LayoutInflater.from(mContext).inflate(R.layout.item_invite_gallary, null);
        ImageView iv_bg = view2.findViewById(R.id.iv_bg);
        iv_bg.setImageResource(R.drawable.shoutu_banner02);
        view2.setOnClickListener(new OnClickBanner());

//        views.add(huodongView);
        views.add(view);
        views.add(view2);
        vpBanner.setAdapter(new ViewPagerAdapter(mContext, views));
        vpBanner.setPlayDelay(10 * 1000);
        vpBanner.setHintView(null);
        vpBanner.getViewPager().setOffscreenPageLimit(2);
        piIndicator.setViewPager(vpBanner.getViewPager());
    }

    private void initTab() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new InviteDiscipleFragment());
        fragmentList.add(new MyDiscipleFragment());
        List<String> titleList = new ArrayList<>();
        titleList.add("邀请收徒");
        titleList.add("我的徒弟");
        pagerAdapter = new MyPagerAdapter(mContext, getChildFragmentManager(), fragmentList, titleList);
        vpViewpager.setAdapter(pagerAdapter);
        tlTablayout.setViewPager(vpViewpager);
        tlTablayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1 && !UserUtils.isLogin()) {
                    vpViewpager.setCurrentItem(0);
                    LoginActivity.open(mContext);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.iv_standard})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_standard:
                AgentWebActivity.openForMoneyCheats(mContext);
                break;
        }
    }

    private class OnClickBanner implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (!UserUtils.isLogin()) {
                LoginActivity.open(mContext);
                return;
            }
            EventBus.getDefault().post(new EventBean(), "InviteDiscipleFragment");
        }
    }

    @Subscriber(tag = "InviteFragment")
    private void onEvent(EventBean eb) {
        switch (eb.getCode()) {
            case BaseConfig.EventCode.SHOW_INVITEDICIPLE:
                vpViewpager.setCurrentItem(0);
                break;
        }
    }

    @Override
    public void onDestroy() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            KumaLog.kuma("mDisposable");
            mDisposable.dispose();
        }
        super.onDestroy();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("InviteFragment");
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("InviteFragment");
    }
}
